package com.xywy.tangandroid.me.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.activity.SetNewPasswordAct;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

/*******************************************
 * @Title: ValidationPswActivity.java
 * @Package com.xywy.tangandroid.me.activity
 * @Description: TODO 验证原密码
 * @author Benwolf
 * @date 2014-12-4 下午3:53:32
 * @version V1.0
 ********************************************/
public class ValidationPswActivity extends BaseActivity {

	private RelativeLayout left_img;
	private TextView middle_title;
	private RelativeLayout rl_validation_psw;
	private EditText primary_password;
	private String mobile;
	private String TAG;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_validation_psw);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(
				R.string.mine_account_primary_validation_psw_str));
		primary_password = (EditText) findViewById(R.id.primary_password);
		rl_validation_psw = (RelativeLayout) findViewById(R.id.rl_validation_psw);
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		left_img.setOnClickListener(this);
		rl_validation_psw.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		switch (view.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.rl_validation_psw:
			validationPrimaryPsw();
			break;
		default:
			break;
		}
	}

	private void validationPrimaryPsw() {
		// TODO Auto-generated method stub
		String psw = primary_password.getText().toString().trim();
		if (mobile.length() < 11) {
			Toast.makeText(context, "获取手机号失败", 1).show();
			return;
		} else if (psw.length() < 6 || psw.length() > 16) {
			Toast.makeText(context, "请输入6~16个字符，只支持数字在，字母区分大小写",
					Toast.LENGTH_SHORT).show();
			return;
		}
		PostRequest<String> postRequest = new PostRequest<String>(Constant.url
				+ "act=kbb&fun=users&type=verifyPassword&sign="
				+ MD5.md5s(Constant.Sign + "TangAndroid") + "&tag=TangAndroid",
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						UIUtil.DissDialog(context, waittingDialog);
						try {
							Log.d(TAG, URLDecoder.decode(response, "utf-8"));
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						if (response != null) {
							JSONObject json;
							try {
								json = new JSONObject(response);
								int state = json.optInt("state");
								if (state == 200) {
									Intent intent = new Intent(context,
											SetNewPasswordAct.class);
									startActivity(intent);
								} else {
									Toast.makeText(context, "请输入正确的密码！",
											Toast.LENGTH_SHORT).show();

								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}

					}
				});
		postRequest.setIsParseJson(false);
		// 封装参数
		Map<String, String> params = new HashMap<String, String>();
		// params.put("type", "verifyPassword");
		params.put("phonenum", mobile.trim());
		params.put("password", psw);
		Log.i("hhah", mobile + "==" + psw);

		postRequest.setParams(params);
		Log.d(TAG, params.toString());
		executeRequest(postRequest);
		// myProgress = new MyProgress(this, 10);
		// myProgress.start();
		// paramsParser = new Parser();

		// 请求路径
		// paramsParser.action = getString(R.string.validate_password);
		// // 请求方式
		// paramsParser.request = "post";

		// 请求参数
		// if (StringUtils.isNull(mobile)) {
		// SharedPreferences sp = sp = getSharedPreferences("config",
		// Context.MODE_PRIVATE);
		// mobile = sp.getString("mobile", "");
		// }
		// params.put("phonenum", mobile.trim());
		// params.put("password", et_password.getText().toString());

		// paramsParser.map = params;
		// 传递上下文
		// paramsParser.context = this.context;

		// asyncHttpClient = AsyncHttpClient.getInstance();
		// asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		SharedPreferences sp = getSharedPreferences("xywy_tang_user",
				Context.MODE_PRIVATE);
		mobile = sp.getString("phonenum", "");
	}

}
